{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os, sys\n",
    "import time\n",
    "import datetime\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import math\n",
    "from math import radians, cos, sin, asin, sqrt \n",
    "import random\n",
    "\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.ticker import MultipleLocator, FormatStrFormatter\n",
    "from matplotlib.patches import Ellipse, Circle\n",
    "\n",
    "ROOTDIR = os.path.abspath(os.path.realpath('./')) + '/Py'\n",
    "\n",
    "sys.path.append(os.path.join(ROOTDIR, ''))\n",
    "\n",
    "import dgckernel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "Zoom=13\n",
    "CALCULATOR = dgckernel.Calculator()\n",
    "CALCULATOR.SetLayer(Zoom)\n",
    "\n",
    "'''Boundary verify'''\n",
    "\n",
    "def GIS_verify(boudary_set,lat,lng):\n",
    "    tag=False\n",
    "    i=-1\n",
    "    j=len(boudary_set)-1\n",
    "    max_lng=max(boudary_set[:,1]);min_lng=min(boudary_set[:,1]);\n",
    "    max_lat=max(boudary_set[:,0]);min_lat=min(boudary_set[:,0]);\n",
    "    \n",
    "    if lng>max_lng or lng<min_lng or lat>max_lat or lat<min_lat:\n",
    "        return tag\n",
    "    \n",
    "    while i<len(boudary_set)-1:\n",
    "        i += 1\n",
    "        if (boudary_set[j][0]<=lat and lat<=boudary_set[i][0]) or (boudary_set[i][0]<=lat and lat<=boudary_set[j][0]):\n",
    "            if (lng<(boudary_set[j][1]-boudary_set[i][1])*(lat-boudary_set[i][0])/(boudary_set[j][0]-boudary_set[i][0])+boudary_set[i][1]):\n",
    "                tag=not tag\n",
    "        j=i \n",
    "    return tag\n",
    "    \n",
    "\n",
    "'''GRID ID'''\n",
    "\n",
    "def get_grid(lng,lat):\n",
    "\n",
    "    return CALCULATOR.HexCellKey(dgckernel.GeoCoord(lat, lng))\n",
    "\n",
    "'''GRID SHAPE'''\n",
    "\n",
    "def get_grid_shape(grid):\n",
    "\n",
    "    return CALCULATOR.HexCellVertexesAndCenter(grid)\n",
    "\n",
    "'''Neighbor Grid'''\n",
    "\n",
    "def grid_neighbor(grid, low_layer, up_layer):\n",
    "\n",
    "    neighbors = CALCULATOR.HexCellNeighbor(grid, up_layer)\n",
    "    _neighbors = CALCULATOR.HexCellNeighbor(grid, low_layer)\n",
    "    neighbors = [e for e in neighbors if e not in _neighbors]\n",
    "    return neighbors\n",
    "\n",
    "'''GRID ELIMINATE'''\n",
    "\n",
    "def grid_eliminate(grid_list,boudary_set):\n",
    "    grid_result=list()\n",
    "    for grid in grid_list:\n",
    "        v_f,c_f=get_grid_shape(grid)\n",
    "        c_lng,c_lat=c_f.lng,c_f.lat;\n",
    "        if GIS_verify(boudary_set,c_lat,c_lng):\n",
    "            grid_result.append(grid)\n",
    "    return grid_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OL13F1i6601j1666\n",
      "104.16687067930721 30.56169449187105\n",
      "104.17096396285251 30.565855575933192\n",
      "104.16893062909418 30.571507116514418\n",
      "104.16280362061165 30.572996964886578\n",
      "104.15871086694568 30.56883549618589\n",
      "104.16074459178789 30.563184563731426\n",
      "104.16483727086298 30.567345737400842\n"
     ]
    }
   ],
   "source": [
    "lat,lng=30.571824,104.166923\n",
    "\n",
    "print(get_grid(lng,lat))\n",
    "\n",
    "\n",
    "grid=get_grid(lng,lat)\n",
    "\n",
    "v_f,c_f=get_grid_shape(grid)\n",
    "\n",
    "\n",
    "'''v_f 为格子的顶点'''\n",
    "\n",
    "'''v_c 为格子的中心点'''\n",
    "\n",
    "print(v_f[0].lng,v_f[0].lat)\n",
    "\n",
    "print(v_f[1].lng,v_f[1].lat)\n",
    "\n",
    "print(v_f[2].lng,v_f[2].lat)\n",
    "\n",
    "print(v_f[3].lng,v_f[3].lat)\n",
    "\n",
    "print(v_f[4].lng,v_f[4].lat)\n",
    "\n",
    "print(v_f[5].lng,v_f[5].lat)\n",
    "\n",
    "print(c_f.lng,c_f.lat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['OL13F1i6600j1654',\n",
       " 'OL13F1i6600j1653',\n",
       " 'OL13F1i6599j1653',\n",
       " 'OL13F1i6598j1654',\n",
       " 'OL13F1i6598j1655',\n",
       " 'OL13F1i6599j1655']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid='OL13F1i6599j1654'\n",
    "\n",
    "low_layer=0\n",
    "\n",
    "'''向外围获取一层'''\n",
    "up_layer=1\n",
    "\n",
    "grid_neighbor(grid, low_layer, up_layer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                       0           1           2           3  \\\n",
      "0       39d471630d26439111a7746d164d34eb  1478091677  1478092890  104.008160   \n",
      "1       6681082cbcc11b37842489a096e2172e  1478093722  1478094806  104.053993   \n",
      "2       56728c3d9555a452307d47986a773d9d  1478081351  1478082003  104.068120   \n",
      "3       5b2724a9fd290d0388264fe07a9aec4c  1478082542  1478083418  104.059534   \n",
      "4       0a2055d9c863e2f1963856d4d305ccd9  1478078909  1478080807  104.079048   \n",
      "...                                  ...         ...         ...         ...   \n",
      "214645  36822254a1843a1389f4eed9457411d4  1478047966  1478049128  104.095899   \n",
      "214646  443f68f1ab13967c46cf72617ae94d2d  1478055208  1478056965  104.013940   \n",
      "214647  d26a8f3e6da4d24180baa3471d5aaf70  1478079323  1478080122  104.116516   \n",
      "214648  05c8202d64efb0943588fade5a46b298  1478049963  1478050925  104.154830   \n",
      "214649  5085b204936c381e4e25566760667f17  1478051309  1478053143  104.107590   \n",
      "\n",
      "                4           5          6     7  \n",
      "0       30.706220  104.064147  30.685848  3.82  \n",
      "1       30.684104  104.015670  30.633230  3.66  \n",
      "2       30.708790  104.049720  30.704970  1.91  \n",
      "3       30.696013  104.034630  30.720960  2.60  \n",
      "4       30.796888  104.083284  30.705019  5.74  \n",
      "...           ...         ...        ...   ...  \n",
      "214645  30.657691  104.072890  30.668350  1.92  \n",
      "214646  30.638610  104.070480  30.655100  3.63  \n",
      "214647  30.642620  104.144240  30.672820  2.68  \n",
      "214648  30.740500  104.107620  30.686210  3.90  \n",
      "214649  30.686080  104.172420  30.652160  5.31  \n",
      "\n",
      "[214650 rows x 8 columns]\n"
     ]
    }
   ],
   "source": [
    "OrderT=pd.read_csv('order_01-30/total_ride_request/order_20161102',header=None)\n",
    "print(OrderT)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                       0           1           2           3  \\\n",
      "0       eb9dd4095d9850e6287cefd813775a6c  1477964797  1477966507  104.094640   \n",
      "1       387a742fa5a3fbe4a1f215ac58ea33a8  1477985585  1477987675  104.076509   \n",
      "2       9cf55f8e6e02a1e0f792df06e5d85011  1478004952  1478006217  104.019699   \n",
      "3       5feeae0307e15203484b9ffceef89855  1477989840  1477991065  104.036090   \n",
      "4       ad4b52cb15b90c44c8f20a8b1e57a622  1477958005  1477958577  104.115997   \n",
      "...                                  ...         ...         ...         ...   \n",
      "209418  8bf1eeef34a502f75b32e37252a6d2f3  1478002701  1478006874  104.049042   \n",
      "209419  b0e7cd3826f12850c35658c431aefca4  1478003623  1478005640  104.064580   \n",
      "209420  bda1578c6390bf0006667d63b5199ddd  1478007320  1478009600  104.031620   \n",
      "209421  8a445f5feb059da8686f39ef0f3b316a  1478010440  1478012878  104.031540   \n",
      "209422  b0e7cd3826f12850c35658c431aefca4  1478003623  1478005640  104.064580   \n",
      "\n",
      "                4           5          6      7  OrgLoc  DstLoc  Time  \n",
      "0       30.703971  104.089270  30.650850   3.54       0       0     0  \n",
      "1       30.767430  104.063700  30.589510  11.70       0       0     0  \n",
      "2       30.689007  104.105324  30.663950   5.01       0       0     0  \n",
      "3       30.622690  104.043860  30.682320   4.22       0       0     0  \n",
      "4       30.652313  104.104421  30.695113   2.27       0       0     0  \n",
      "...           ...         ...        ...    ...     ...     ...   ...  \n",
      "209418  30.710625  104.027720  30.636250   6.47       0       0     0  \n",
      "209419  30.708090  104.031050  30.688930   3.63       0       0     0  \n",
      "209420  30.643860  104.017890  30.737160   6.19       0       0     0  \n",
      "209421  30.718300  104.120730  30.862670  11.54       0       0     0  \n",
      "209422  30.708090  104.031050  30.688930   3.63       0       0     0  \n",
      "\n",
      "[209423 rows x 11 columns]\n"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                       0           1           2           3  \\\n",
      "0       eb9dd4095d9850e6287cefd813775a6c  1477964797  1477966507  104.094640   \n",
      "1       387a742fa5a3fbe4a1f215ac58ea33a8  1477985585  1477987675  104.076509   \n",
      "2       9cf55f8e6e02a1e0f792df06e5d85011  1478004952  1478006217  104.019699   \n",
      "3       5feeae0307e15203484b9ffceef89855  1477989840  1477991065  104.036090   \n",
      "4       ad4b52cb15b90c44c8f20a8b1e57a622  1477958005  1477958577  104.115997   \n",
      "...                                  ...         ...         ...         ...   \n",
      "209418  8bf1eeef34a502f75b32e37252a6d2f3  1478002701  1478006874  104.049042   \n",
      "209419  b0e7cd3826f12850c35658c431aefca4  1478003623  1478005640  104.064580   \n",
      "209420  bda1578c6390bf0006667d63b5199ddd  1478007320  1478009600  104.031620   \n",
      "209421  8a445f5feb059da8686f39ef0f3b316a  1478010440  1478012878  104.031540   \n",
      "209422  b0e7cd3826f12850c35658c431aefca4  1478003623  1478005640  104.064580   \n",
      "\n",
      "                4           5          6      7            OrgLoc  \\\n",
      "0       30.703971  104.089270  30.650850   3.54  OL13F1i6594j1656   \n",
      "1       30.767430  104.063700  30.589510  11.70                 0   \n",
      "2       30.689007  104.105324  30.663950   5.01                 0   \n",
      "3       30.622690  104.043860  30.682320   4.22                 0   \n",
      "4       30.652313  104.104421  30.695113   2.27                 0   \n",
      "...           ...         ...        ...    ...               ...   \n",
      "209418  30.710625  104.027720  30.636250   6.47                 0   \n",
      "209419  30.708090  104.031050  30.688930   3.63                 0   \n",
      "209420  30.643860  104.017890  30.737160   6.19                 0   \n",
      "209421  30.718300  104.120730  30.862670  11.54                 0   \n",
      "209422  30.708090  104.031050  30.688930   3.63                 0   \n",
      "\n",
      "                  DstLoc  \n",
      "0       OL13F1i6600j1656  \n",
      "1                      0  \n",
      "2                      0  \n",
      "3                      0  \n",
      "4                      0  \n",
      "...                  ...  \n",
      "209418                 0  \n",
      "209419                 0  \n",
      "209420                 0  \n",
      "209421                 0  \n",
      "209422                 0  \n",
      "\n",
      "[209423 rows x 10 columns]\n"
     ]
    }
   ],
   "source": [
    "# lng=OrderT.iloc[0,3]\n",
    "# lat=OrderT.iloc[0,4]\n",
    "# OrderT.iloc[0,8]=get_grid(lng,lat)\n",
    "# lng=OrderT.iloc[0,5]\n",
    "# lat=OrderT.iloc[0,6]\n",
    "# OrderT.iloc[0,9]=get_grid(lng,lat)\n",
    "# print(OrderT)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['OL13F1i6595j1656',\n",
       " 'OL13F1i6595j1655',\n",
       " 'OL13F1i6594j1655',\n",
       " 'OL13F1i6593j1656',\n",
       " 'OL13F1i6593j1657',\n",
       " 'OL13F1i6594j1657']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid=OrderT.iloc[0,8]\n",
    "\n",
    "low_layer=0\n",
    "\n",
    "'''向外围获取一层'''\n",
    "up_layer=1\n",
    "\n",
    "grid_neighbor(grid, low_layer, up_layer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''Time stamp'''\n",
    "def stamp_transit(time_str):\n",
    "    timeArray = time.strptime(time_str, \"%Y-%m-%d %H:%M:%S\")\n",
    "    timeStamp = int(time.mktime(timeArray))\n",
    "    return timeStamp\n",
    "    \n",
    "'''Time step'''\n",
    "\n",
    "def stamp_to_step(timestamp,date_str,step):\n",
    "    baseline = date_str+\" 00:00:00\";\n",
    "    baseline = int(stamp_transit(baseline))\n",
    "    current_step=int((timestamp-baseline)/step)\n",
    "    return current_step\n",
    "date_str=\"2016-11-02\"\n",
    "OrderT['Start_step']=OrderT.apply(lambda x:stamp_to_step(x[1],date_str,300), axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                       0           1           2           3  \\\n",
      "0       39d471630d26439111a7746d164d34eb  1478091677  1478092890  104.008160   \n",
      "1       6681082cbcc11b37842489a096e2172e  1478093722  1478094806  104.053993   \n",
      "2       56728c3d9555a452307d47986a773d9d  1478081351  1478082003  104.068120   \n",
      "3       5b2724a9fd290d0388264fe07a9aec4c  1478082542  1478083418  104.059534   \n",
      "4       0a2055d9c863e2f1963856d4d305ccd9  1478078909  1478080807  104.079048   \n",
      "...                                  ...         ...         ...         ...   \n",
      "214645  36822254a1843a1389f4eed9457411d4  1478047966  1478049128  104.095899   \n",
      "214646  443f68f1ab13967c46cf72617ae94d2d  1478055208  1478056965  104.013940   \n",
      "214647  d26a8f3e6da4d24180baa3471d5aaf70  1478079323  1478080122  104.116516   \n",
      "214648  05c8202d64efb0943588fade5a46b298  1478049963  1478050925  104.154830   \n",
      "214649  5085b204936c381e4e25566760667f17  1478051309  1478053143  104.107590   \n",
      "\n",
      "                4           5          6     7  Start_step  \n",
      "0       30.706220  104.064147  30.685848  3.82         108  \n",
      "1       30.684104  104.015670  30.633230  3.66         115  \n",
      "2       30.708790  104.049720  30.704970  1.91          73  \n",
      "3       30.696013  104.034630  30.720960  2.60          77  \n",
      "4       30.796888  104.083284  30.705019  5.74          65  \n",
      "...           ...         ...        ...   ...         ...  \n",
      "214645  30.657691  104.072890  30.668350  1.92         -37  \n",
      "214646  30.638610  104.070480  30.655100  3.63         -13  \n",
      "214647  30.642620  104.144240  30.672820  2.68          67  \n",
      "214648  30.740500  104.107620  30.686210  3.90         -30  \n",
      "214649  30.686080  104.172420  30.652160  5.31         -26  \n",
      "\n",
      "[214650 rows x 9 columns]\n"
     ]
    }
   ],
   "source": [
    "print(OrderT)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1478059200\n"
     ]
    }
   ],
   "source": [
    "print(stamp_transit(\"2016-11-02 00:00:00\"))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
